package cn.imokay.juc.captainSynchronized;

/**
 * 方法锁的锁对象默认是this,方法锁，锁住的时整个类的同步方法资源
 */
public class MethodLock implements Runnable {

    static MethodLock methodLock = new MethodLock();
    static MethodLock methodLock2 = new MethodLock();

    @Override
    public void run() {
        try {
            method();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private synchronized void method() throws InterruptedException {
        Thread.sleep(3000);
        System.out.println(Thread.currentThread().getName());
        System.out.println(Thread.currentThread().getName() + "结束");

    }

    public static void main(String[] args) {
        Thread t1 = new Thread(methodLock);
        Thread t2 = new Thread(methodLock);
        t1.start();
        t2.start();

        while (t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("finish");
    }
}
